#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QRegularExpression>
#include <QChar>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_1_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text().append("1"));

}


void MainWindow::on_pushButton_2_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text().append("2"));
}


void MainWindow::on_pushButton_3_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text().append("3"));
}


void MainWindow::on_pushButton_4_clicked()
{

    ui->lineEdit->setText(ui->lineEdit->text().append("4"));
}


void MainWindow::on_pushButton_5_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text().append("5"));
}


void MainWindow::on_pushButton_6_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text().append("6"));
}


void MainWindow::on_pushButton_7_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text().append("7"));
}


void MainWindow::on_pushButton_8_clicked()
{

    ui->lineEdit->setText(ui->lineEdit->text().append("8"));
}


void MainWindow::on_pushButton_9_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text().append("9"));
}


void MainWindow::on_pushButton_square_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text().append("^2"));
}


void MainWindow::on_pushButton_0_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text().append("0"));
}


void MainWindow::on_pushButton_precent_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text().append("%"));
}


void MainWindow::on_pushButton_plus_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text().append("+"));
}


void MainWindow::on_pushButton_minus_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text().append("-"));
}


void MainWindow::on_pushButton_clear_clicked()
{
    ui->lineEdit->clear();
}


void MainWindow::on_pushButton_multiplication_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text().append("*"));
}


void MainWindow::on_pushButton_division_clicked()
{
    ui->lineEdit->setText(ui->lineEdit->text().append("/"));
}


void MainWindow::on_pushButton_equal_clicked()
{
    QString formula = ui->lineEdit->text();
    qDebug() << formula;
    QChar opera;
    if(formula.contains('+'))
    {
        opera = '+';
        flags = 1;
    }
    if(formula.contains('-'))
    {
        opera = '-';
        flags = 2;
    }
    if(formula.contains('*'))
    {
        opera = '*';
        flags = 3;
    }
    if(formula.contains('/'))
    {
        opera = '/';
        flags = 4;
    }

    QStringList list = formula.split(opera);
    int result = 0;
    switch (flags)
    {
    case 1:
    {
        for(int i = 0; i < list.size(); i++)
            result += list.at(i).toInt();
        break;
    }
    case 2:
    {
        for(int i = 0; i < list.size(); i++)
            result -= list.at(i).toInt();
        break;
    }
    case 3:
    {
        result = 1;
        for(int i = 0; i < list.size(); i++)
            result *= list.at(i).toInt();
        break;
    }
    case 4:
    {
        result = list.at(0).toInt()*list.at(0).toInt();
        for(int i = 0; i < list.size(); i++)
            result /= list.at(i).toInt();
        break;
    }
    }

    ui->lineEdit->setText(QString("%1").arg(result));

}


void MainWindow::on_lineEdit_textChanged()
{
//    QString str = ui->lineEdit->text();
//    QRegularExpression exp("^[0-9]*$");
//    if(!exp.match(str).hasMatch())
//    {
//        qDebug() << "wrong";
//    }
}

